package org.jeecg.modules.wangyue.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.*;
import org.jeecg.modules.wangyue.entity.IndentJs;
import org.jeecg.modules.wangyue.entity.UserBasic;
import org.jeecg.modules.wangyue.entity.UserRoleInfo;
import org.jeecg.modules.wangyue.service.IIndentJsService;
import org.jeecg.modules.wangyue.service.IUserBasicService;
import org.jeecg.modules.wangyue.service.IUserRoleInfoService;
import org.jeecg.modules.wangyue.utils.ExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @Description: 技师管理
* @Author: jeecg-boot
* @Date:   2023-06-30
* @Version: V1.0
*/
@Slf4j
@Api(tags="技师管理")
@RestController
@RequestMapping("/wangyue/sys/jsManage")
public class JsManageController extends JeecgController<UserRoleInfo, IUserRoleInfoService> {
   @Autowired
   private IUserRoleInfoService userRoleInfoService;
   @Autowired
   private IIndentJsService indentJsService;
   @Autowired
   private ExportUtil exportUtil;

    /**
     * 技师管理-分页列表查询
     * @param keywords		关键字
     * @param pageNo
     * @param pageSize
     * @return
     */
   @AutoLog(value = "技师管理-分页列表查询")
   @ApiOperation(value="技师管理-分页列表查询", notes="技师管理-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(@RequestParam(name="keywords", required = false) String keywords,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
       Page<UserRoleInfo> page = new Page<UserRoleInfo>(pageNo, pageSize);
       IPage<UserRoleInfo> pageList = userRoleInfoService.getJsRoleInfoByPage(page, RoleConstant.jishi, keywords, IsRoleAuthConstant.yes);
       return Result.OK(pageList);
   }



    /**
     * 技师管理-查询技师订单收益
     * @param jsUserId		技师userId
     * @return
     */
    @AutoLog(value = "技师管理-查询技师订单收益")
    @ApiOperation(value="技师管理-查询技师订单收益", notes="技师管理-查询技师订单收益")
    @GetMapping(value = "/findJsIndentTotalAmount")
    public Result<?> findJsIndentTotalAmount(@RequestParam(name="jsUserId", required = true) String jsUserId) {
        String finishIndentTotalAmount = "0";
        QueryWrapper<IndentJs> indentJsQueryWrapper = new QueryWrapper<>();
        String finishIndentTotalAmountSql = "SUM(actual_amount) as finishIndentTotalAmount";
        indentJsQueryWrapper.select(finishIndentTotalAmountSql);
        indentJsQueryWrapper.eq("taking_user_id", jsUserId);
        indentJsQueryWrapper.eq("taking_user_type", RoleConstant.jishi);
        indentJsQueryWrapper.eq("order_status", OrderStatusConstant.serviceCompletion);
        indentJsQueryWrapper.eq("del_flag", DelFlagConstant.normal);
        Map ijMap = indentJsService.getMap(indentJsQueryWrapper);
        if(oConvertUtils.isNotEmpty(ijMap)){ ;
            finishIndentTotalAmount = ijMap.get("finishIndentTotalAmount") + "";
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("finishIndentTotalAmount", finishIndentTotalAmount);
        return Result.OK(jsonObject);
    }


    /**
     * 导出excel
     *
     * @param userRoleInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(UserRoleInfo userRoleInfo) {
        List<String> idList = null;
        if(oConvertUtils.isNotEmpty(userRoleInfo.getIds())){
            idList = Arrays.asList(userRoleInfo.getIds().split(","));
        }
        List<UserRoleInfo> userRoleInfoList = userRoleInfoService.getJsRoleInfoByList(RoleConstant.jishi, idList, userRoleInfo.getKeywords(), IsRoleAuthConstant.yes);

        //处理url数据，添加文件地址前缀
        if(oConvertUtils.isNotEmpty(userRoleInfoList) && userRoleInfoList.size() > 0){
            for(UserRoleInfo uri : userRoleInfoList){
                if (oConvertUtils.isNotEmpty(uri.getHeadPicUrl())){
                    uri.setHeadPicUrl(CommonConstant.fileAddressPrefix + uri.getHeadPicUrl());
                }
                if (oConvertUtils.isNotEmpty(uri.getIdCardFrontUrl())){
                    uri.setIdCardFrontUrl(CommonConstant.fileAddressPrefix + uri.getIdCardFrontUrl());
                }
                if (oConvertUtils.isNotEmpty(uri.getIdCardBackUrl())){
                    uri.setIdCardBackUrl(CommonConstant.fileAddressPrefix + uri.getIdCardBackUrl());
                }
                if (oConvertUtils.isNotEmpty(uri.getWorkProveUrl())){
                    uri.setWorkProveUrl(CommonConstant.fileAddressPrefix + uri.getWorkProveUrl());
                }
                if (oConvertUtils.isNotEmpty(uri.getHealthProveUrl())){
                    uri.setHealthProveUrl(CommonConstant.fileAddressPrefix + uri.getHealthProveUrl());
                }
            }
        }

        return exportUtil.exportXls(userRoleInfoList, UserRoleInfo.class, "技师信息数据");
    }


}
